feat: add SWR local storage cache layer for offline dashboard resiliency (#2801) - #3348
feat: add SWR local storage cache layer for offline dashboard resiliency (#2801)#3348yachikadev wants to merge 8 commits into
Conversation
Adds saveSnapshot/getSnapshot/clearSnapshot helpers with try-catch guards so localStorage failures (disabled, quota full, SSR context) never throw at runtime.
Adds saveSnapshot/getSnapshot/clearSnapshot helpers with try-catch guards so localStorage failures (disabled, quota full, SSR context) never throw at runtime.
- Load last cached streak/contribution data from localStorage on mount before the network request resolves - Persist fresh data to localStorage on successful fetch - Fall back to cached snapshot instead of showing an error when the live fetch fails (offline / rate-limited) Part of Priyanshu-byte-coder#2801
GSSoC Label Checklist 🏷️@Priyanshu-byte-coder — please apply the appropriate labels before merging: Difficulty (pick one):
Quality (optional):
Validation (required to score):
|
- Load last cached goals list from localStorage on mount - Persist fresh goals to localStorage on successful load - Fall back to cached goals instead of an error message when the live fetch fails Closes Priyanshu-byte-coder#2801
7675cab to
5e31350
Compare
|
Good PR — the stale-while-revalidate approach is right: instant hydration from One thing to fix before I merge — two inline comments are in Hindi: Code comments in this repo are English-only so every contributor can read them. Please reword those two (e.g. |
Summary
Implements a Stale-While-Revalidate (SWR) local storage cache layer for the dashboard's Streak Tracker and Goal Tracker widgets, so users see their last-known data instantly on load instead of a blank/loading state — even on spotty networks, during GitHub API rate-limiting, or fully offline.
Closes #2801
Changes
src/lib/localCache.ts(new) — lightweightsaveSnapshot/getSnapshot/clearSnapshothelpers backed bylocalStorage, withtry/catchguards so a full, disabled, or missinglocalStoragenever throws at runtime.src/components/StreakTracker.tsx— on mount, hydrates instantly from the last cached streak/contribution snapshot before the network call resolves. On successful fetch, persists the new snapshot. On fetch failure, falls back to the cached snapshot instead of showing an error.src/components/GoalTracker.tsx— same pattern applied to the goals list: instant hydration from cache, snapshot saved on successful load, cached goals shown if the live fetch fails.How it works
useEffecton mount reads the cached snapshot fromlocalStorageand populates state immediately, skipping the loading spinner if data is available.Notes
/api/metricsand/api/goals(streaks + goals), matching the issue's acceptance criteria. Other widgets are untouched and can adopt the samelocalCache.tsutility in future PRs if desired.